package com.tsd.jxc.stock.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.entity.JxcStockOutbillLogistics;
import com.tsd.jxc.stock.entity.JxcStockOutbillLogisticsExt;
import com.tsd.jxc.stock.service.JxcStockOutbillLogisticsService;
import com.tsd.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * 描述：JxcStockOutbillLogistics出库单物流信息 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-出库单物流信息", description = "出库单物流信息的API描述")
public class JxcStockOutbillLogisticsController extends BaseController {

    @Resource
    private JxcStockOutbillLogisticsService jxcStockOutbillLogisticsService;

    @ApiOperation(value = "根据sid获取出库单物流信息", notes = "根据sid获取出库单物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "出库单物流信息sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcStockOutbillLogisticsBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getJxcStockOutbillLogisticsBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcStockOutbillLogistics jxcStockOutbillLogistics = jxcStockOutbillLogisticsService.queryBySid(sid);
            hr.setData(jxcStockOutbillLogistics);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除出库单物流信息", notes = "根据id删除出库单物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "出库单物流信息sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcStockOutbillLogisticss.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcStockOutbillLogisticss() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcStockOutbillLogisticsService.deleteJxcStockOutbillLogisticss(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存出库单物流信息", notes = "保存出库单物流信息，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "出库单物流信息", required = true, paramType = "body", dataType = "JxcStockOutbillLogistics")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcStockOutbillLogistics.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcStockOutbillLogistics() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcStockOutbillLogistics t = JSON.parseObject(json, JxcStockOutbillLogistics.class);
            jxcStockOutbillLogisticsService.saveJxcStockOutbillLogistics(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询出库单物流信息", notes = "分页查询出库单物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcStockOutbillLogisticss.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcStockOutbillLogisticss() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<JxcStockOutbillLogisticsExt> list = jxcStockOutbillLogisticsService.findJxcStockOutbillLogisticss(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取所有出库单物流信息", notes = "获取所有出库单物流信息")
    @ApiImplicitParams({
    })
    @ResponseBody
    @RequestMapping(value = "/getAllLogistics.action", method = RequestMethod.POST)
    public HttpResult<Object> getAllLogistics() {
        HttpResult<java.lang.Object> hr = new HttpResult<>();
        try {
            Map<String, java.lang.Object> map = super.getParams(request);
            Map<String, java.lang.Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyStringValue(map, params, "code", "code", false, true);
            PageBean page = new PageBean(map);
            List<JxcStockOutbillLogisticsExt> list = jxcStockOutbillLogisticsService.getAllLogisticsByPage(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询出库单物流信息", notes = "查询出库单物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "出库单物流信息sid", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "sent_date", value = "出库单物流信息sent_date", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "recv_phone", value = "出库单物流信息recv_phone", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "recv_name", value = "出库单物流信息recv_name", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "logistics_bill_no", value = "出库单物流信息logistics_bill_no", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/queryLogistics.action", method = RequestMethod.POST)
    public HttpResult<Object> queryLogistics() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            List<JxcStockOutbillLogisticsExt> list = new ArrayList<>();
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyStringValue(map, params, "code", "code", false, false);
            HlpUtils.copyStringValue(map, params, "outbill_sid", "outbill_sid", false, false);
            HlpUtils.copyStringValue(map, params, "outbill_code", "outbill_code", false, false);
            HlpUtils.copyStringValue(map, params, "logistics_bill_no", "logistics_bill_no", false, false);
            HlpUtils.copyStringValue(map, params, "begin_date", "begin_date", false, false);
            HlpUtils.copyStringValue(map, params, "end_date", "end_date", false, false);
            HlpUtils.copyStringValue(map, params, "recv_name", "recv_name", false, false);
            HlpUtils.copyStringValue(map, params, "recv_phone", "recv_phone", false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            // 发货日期
            String sent_date = HlpUtils.getString(map, "sent_date");
            if (!HlpUtils.isEmpty(sent_date)) {
                // 发货日期 开始日期、结束日期
                this.dateRange(map, params, "sent_date");
            }
            PageBean page = new PageBean(map);
            list = jxcStockOutbillLogisticsService.selectLogisticsByPage(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 获取 key 开始时间、结束时间
     *
     * @param map
     * @param params
     * @param key
     * @throws ParseException
     */
    private void dateRange(Map<String, Object> map, Map<String, Object> params, String key) throws ParseException {
        if (!HlpUtils.isEmpty(key)) {
            // 实际发货日期
            String value = HlpUtils.getString(map, key);
            if (!HlpUtils.isEmpty(value)) {
                if (JSONUtil.isJsonArray(value)) {
                    List<String> value_list = JSON.parseArray(value, String.class);
                    // 要求发货开始日期、要求发货结束日期
                    if (value_list != null && value_list.size() == 2) {
                        Date date_begin = HlpUtils.string2Date(value_list.get(0), "yyyy-MM-dd");
                        Date date_end = HlpUtils.string2Date(value_list.get(1), "yyyy-MM-dd");
                        date_end = HlpUtils.getNextDate(date_end, 1);
                        params.put(key + "_begin", date_begin);
                        params.put(key + "_end", date_end);
                    }
                } else {
                    params.put(key, value);
                }
            }
        }
    }

    @ApiOperation(value = "根据outbill_sid获取出库单物流信息", notes = "根据outbill_sid获取出库单物流信息")
    @ResponseBody
    @RequestMapping(value = "/queryLogisticsByOutbillSid.action", method = RequestMethod.POST)
    public HttpResult<Object> queryLogisticsByOutbillSid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String outbill_sid = HlpUtils.getString(map, "outbill_sid");
            super.checkEmpty(outbill_sid, "请提供参数：outbill_sid");
            JxcStockOutbillLogisticsExt logisticsExt = jxcStockOutbillLogisticsService.queryLogisticsByOutbillSid(outbill_sid);
            hr.setData(logisticsExt);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
